<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{capture}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.builtin.functions.html" title="Chapter 7. Built-in Functions">
<link rel="prev" href="language.function.call.html" title="{call}">
<link rel="next" href="language.function.config.load.html" title="{config_load}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{capture}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.call.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Built-in Functions</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.config.load.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{capture}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.capture"></a>{capture}</h2></div></div></div>
<p>
   <code class="varname">{capture}</code> is used to collect the output of the template between the
   tags into a  variable instead of displaying it. Any content between
   <code class="varname">{capture name='foo'}</code> and <code class="varname">{/capture}</code> is collected
   into the variable specified  in the <em class="parameter"><code>name</code></em> attribute.
   </p>
<p>The captured content can be used in the
   template from the variable <a class="link" href="language.variables.smarty.html#language.variables.smarty.capture" title="{$smarty.capture}"><em class="parameter"><code>$smarty.capture.foo</code></em></a>
   where <span class="quote">“<span class="quote">foo</span>”</span> is the value passed in the <em class="parameter"><code>name</code></em> attribute.
   If you do not supply the <em class="parameter"><code>name</code></em> attribute, then <span class="quote">“<span class="quote">default</span>”</span> will
   be used as the name ie <em class="parameter"><code>$smarty.capture.default</code></em>.
   </p>
<p><code class="varname">{capture}'s</code> can be nested.
   </p>
<p><span class="bold"><strong>Attributes:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Attribute Name</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">name</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The name of the captured block</td>
</tr>
<tr>
<td align="center">assign</td>
<td align="center">string</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The variable name where to assign the captured output to</td>
</tr>
<tr>
<td align="center">append</td>
<td align="center">string</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The name of an array variable where to append the captured output to</td>
</tr>
</tbody>
</table></div>
<p><span class="bold"><strong>Option Flags:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Name</th>
<th>Description</th>
</tr></thead>
<tbody><tr>
<td align="center">nocache</td>
<td>Disables caching of this captured block</td>
</tr></tbody>
</table></div>
<div class="note" title="Caution" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Caution</h3>
<p>
   Be careful when capturing <a class="link" href="language.function.insert.html" title="{insert}"><code class="varname">{insert}</code></a>
   output. If you have
   <a class="link" href="caching.html" title="Chapter 15. Caching"><em class="parameter"><code>$caching</code></em></a>
   enabled and you have
   <a class="link" href="language.function.insert.html" title="{insert}"><code class="varname">{insert}</code></a>
   commands that you expect to run
   within cached content, do not capture this content.
  </p>
</div>
<p>
  </p>
<div class="example">
<a name="id368523"></a><p class="title"><b>Example 7.21. {capture} with the name attribute</b></p>
<div class="example-contents"><pre class="programlisting">

{* we don't want to print a div tag unless content is displayed *}
{capture name="banner"}
{capture "banner"} {* short-hand *}
  {include file="get_banner.tpl"}
{/capture}

{if $smarty.capture.banner ne ""}
&lt;div id="banner"&gt;{$smarty.capture.banner}&lt;/div&gt;
{/if}

   </pre></div>
</div>
<p><br class="example-break">

  </p>
<div class="example">
<a name="id368535"></a><p class="title"><b>Example 7.22. {capture} into a template variable</b></p>
<div class="example-contents">
<p>This example demonstrates the capture function.</p>
<pre class="programlisting">

{capture name=some_content assign=popText}
{capture some_content assign=popText} {* short-hand *}
The server is {$my_server_name|upper} at {$my_server_addr}&lt;br&gt;
Your ip is {$my_ip}.
{/capture}
&lt;a href="#"&gt;{$popText}&lt;/a&gt;

     </pre>
</div>
</div>
<p><br class="example-break">
  
  </p>
<div class="example">
<a name="id368556"></a><p class="title"><b>Example 7.23. {capture} into a template array variable</b></p>
<div class="example-contents">
<p>
    This example also demonstrates how multiple calls of capture can be used to create an array with captured content.
   </p>
<pre class="programlisting">

{capture append="foo"}hello{/capture}I say just {capture append="foo"}world{/capture}
{foreach $foo as $text}{$text} {/foreach}

     </pre>
<p>
    The above example will output:
   </p>
<pre class="screen">

I say just hello world

  </pre>
</div>
</div>
<p><br class="example-break">
  
  
 </p>
<p>
  See also
  <a class="link" href="language.variables.smarty.html#language.variables.smarty.capture" title="{$smarty.capture}"><em class="parameter"><code>$smarty.capture</code></em></a>,
   <a class="link" href="language.function.eval.html" title="{eval}"><code class="varname">{eval}</code></a>,
  <a class="link" href="language.function.fetch.html" title="{fetch}"><code class="varname">{fetch}</code></a>,
  <a class="link" href="api.fetch.html" title="fetch()"><code class="varname">fetch()</code></a>
  and <a class="link" href="language.function.assign.html" title="{assign}"><code class="varname">{assign}</code></a>.
 </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.call.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.builtin.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.config.load.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{call} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {config_load}</td>
</tr>
</table>
</div>
</body>
</html>
